package com.web;

import com.domain.Employee;
import com.domain.User;
import com.service.employeeService;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 客户忘记密码
 * 接收 updateUserPassword.jsp页面 的 userName、phone、password1 数据
 * 提交到 userService 的 findEmpNamePass函数进行查询用户，updateUserPass函数进行修改
 * 转发到 /LoginUser.jsp 页面进行登录
 */
@WebServlet("/updateUserPass")//进行员工的密码设置
public class updateUserPassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取值
        String userName = request.getParameter("userName");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password1");
        System.out.println("员工"+ userName +" 正在修改密码");
        //3.进行查询判断有没有这个客户的姓名和号码
        User user = new userService().findUserNamePass(userName, phone);
        //4.进行判断
        PrintWriter writer = response.getWriter();
        if (user.getPassword()==password){
            //提示信息
            System.out.println("修改失败");
            writer.write("<h1 align=\"center\">有没有搞错,你的新密码和旧密码是一样的欸!<br> <a href=\"loginEmployee.jsp\"\">去登陆</a></h1>" );
        }else {
            //进行修改密码
            System.out.println("进行修改成功");
            new userService().updateUserPass(password,userName,phone);
            response.sendRedirect(request.getContextPath()+"/LoginUser.jsp");
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
